<template>
	<div class="login_bg">
		<div class="login_main">
			<div class="login-form">
				<div class="login-header">
					<div class="logo">
						<label>低/无代码开发中心</label>
					</div>
				</div>
				<el-form ref="loginForm" :model="form" :rules="rules" label-width="0" size="large" @keyup.enter="login">
          <el-form-item prop="user">
            <el-input v-model="form.user" prefix-icon="el-icon-user" clearable :placeholder="$t('login.userPlaceholder')"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="form.password" prefix-icon="el-icon-lock" clearable show-password :placeholder="$t('login.PWPlaceholder')"></el-input>
          </el-form-item>
          <el-form-item prop="captcha" v-if="captchaEnabled">
            <el-input v-model="form.code" prefix-icon="el-icon-lock" clearable show-password :placeholder="$t('login.CODElaceholder')" style="width: 63%"></el-input>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img" />
            </div>
          </el-form-item>
          <el-form-item style="margin-bottom: 20px">
            <el-col :span="12">
              <el-checkbox :label="$t('login.rememberMe')" v-model="form.autologin"></el-checkbox>
            </el-col>
            <el-col :span="12" class="login-forgot">
              
            </el-col>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 100%" :loading="islogin" round @click="login">{{ $t("login.signIn") }}</el-button>
          </el-form-item>
        </el-form>
			</div>
		</div>
	</div>
</template>
  
<script>
  export default {
  data() {
    return {
      form: {
        uuid: "",
        user: "",
        password: "",
        code: "",
        autologin: false
      },
      rules: {
        user: [{ required: true, message: this.$t("login.userError"), trigger: "blur" }],
        password: [{ required: true, message: this.$t("login.PWError"), trigger: "blur" }]
      },
      islogin: false,
      codeUrl: "",
      captchaEnabled: true
    }
  },
  watch: {},
  mounted() {
    document.title = "应用开发者服务中心"
    this.getCode();
  },
  methods: {
    async getCode() {
      var res = await this.$API.auth.getCodeImg.get()
      if (res.code == 200) {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img
          this.form.uuid = res.uuid
        }
      }
    },
    async login() {
      var validate = await this.$refs.loginForm.validate().catch(() => {})
      if (!validate) {
        return false
      }

      this.islogin = true
      var data = {
        username: this.form.user,
        password: this.form.password, //this.$TOOL.crypto.MD5(this.form.password)
        code: this.form.code,
        uuid: this.form.uuid
      }
      //获取token
      var user = await this.$API.auth.login.post(data)
      if (user.code == 200) {
        this.$TOOL.cookie.set("TOKEN", user.data.access_token, {
          expires: this.form.autologin ? 24 * 60 * 60 : 0
        })
        //获取用户信息
        var userInfo = await this.$API.system.user.getInfo.get()
        this.$TOOL.data.set("USER_INFO", userInfo)
        this.$TOOL.data.set("PERMISSIONS", userInfo.permissions)
      } else {
        this.islogin = false
        if (this.captchaEnabled) {
          this.getCode()
        }
        this.$message.warning(user.msg)
        return false
      }
      //获取菜单
      var menu = null
      menu = await this.$API.system.menu.getMenus.get()

      console.log(menu, "menu")

      if (menu.code == 200) {
        if (menu.data.length == 0) {
          this.islogin = false
          this.$alert("当前用户无任何菜单权限，请联系系统管理员", "无权限访问", {
            type: "error",
            center: true
          })
          return false
        }
        this.$TOOL.data.set("MENU", menu.data)
        //this.$TOOL.data.set("DASHBOARDGRID", menu.data.dashboardGrid)
      } else {
        this.islogin = false
        this.$message.warning(menu.msg)
        return false
      }

      this.$router.replace({
        path: "/"
      })
      this.$message.success("登录成功")
      this.islogin = false
    }
  }
}
</script>

<style scoped>
.login_bg {width: 100%;height: 100%;background: #fff;display: flex; background: url("~@/assets/imgs/bg-login.png");background-repeat: no-repeat;background-size: cover;}
.login_main {flex: 1;overflow: auto;display:flex; }
.login-form {width: 520px;margin: auto;padding:55px; background: #FFF; border-radius: 12px;}
.login-header {margin-bottom: 60px;}
.login-header .logo {display: flex;align-items: center; justify-content: center;}
.login-header .logo img {width: 40px;height: 40px;vertical-align: bottom;margin-right: 10px;}
.login-header .logo label {font-size: 26px;font-weight: bold;}
.login-oauth {text-align: center;}
.login-form .el-divider {margin-top:40px;}

.login-form {}
.login-form:deep(.el-tabs) .el-tabs__header {margin-bottom: 25px;}
.login-form:deep(.el-tabs) .el-tabs__header .el-tabs__item {font-size: 14px;}

.login-form:deep(.login-forgot) {text-align: right;}
.login-form:deep(.login-forgot) a {color: var(--el-color-primary);}
.login-form:deep(.login-forgot) a:hover {color: var(--el-color-primary-light-3);}
.login-form:deep(.login-reg) {font-size: 14px;color: var(--el-text-color-primary);}
.login-form:deep(.login-reg) a {color: var(--el-color-primary);}
.login-form:deep(.login-reg) a:hover {color: var(--el-color-primary-light-3);}

.login_config {position: absolute;top:20px;right: 20px;}

.login-form:deep(.login-msg-yzm) {display: flex;width: 100%;}
.login-form:deep(.login-msg-yzm) .el-button {margin-left: 10px;--el-button-size:42px;}

@media (max-width: 768px){
  .login-form {width: 340px; margin-top: 120px;}
}

@media (max-width: 1200px){
  .login-form {width: 340px;}
}
@media (max-width: 1000px){
  .login_main {display: block;}
  .login_main .login_config {position: static;padding:20px 20px 0 20px;text-align: right;}
  .login-form {padding:20px 40px;}
}
</style>